<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <el-row>
        <i class="el-icon-search"></i>
        <span class="search-title">筛选条件</span>
      </el-row>
      <el-row class="row-margin">
        <el-form :inline="true" :model="searchform" class="demo-form-inline">
          <el-form-item>
            <el-date-picker
              v-model="searchform.date"
              type="datetimerange"
              range-separator="-"
              start-placeholder="请选择"
              end-placeholder="交易时间段"
            ></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-input placeholder="子订单号" v-model="searchform.orderId"></el-input>
          </el-form-item>
          <el-form-item>
            <el-row type="flex">
              <el-select v-model="searchform.supplierId" placeholder="全部状态">
                <el-option
                  v-for="item in supplierList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
              <el-button type="primary" @click="handleSearch" style="margin-left:10px">查询</el-button>
              <el-button @click="handleReset" style="margin-left:10px">重置</el-button>
            </el-row>
          </el-form-item>
        </el-form>
      </el-row>
    </el-card>

    <el-table :data="productList" style="width: 100%" border ref="productListInfo">
      <el-table-column label="订单编号" width="180" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.unit }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易时间" prop="stock" align="center"></el-table-column>
      <el-table-column label="销售金额" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="showSalesModal(scope.$index, scope.row)"
          >{{scope.row.sales}}</el-button>
        </template>
      </el-table-column>

      <el-table-column label="供货价格" prop="supplyPrice" align="center"></el-table-column>

      <el-table-column label="团长分润" prop="supplierName" align="center"></el-table-column>

      <el-table-column label="订单状态" align="center">
        <!-- <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        </template>-->
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row type="flex" justify="end" class="pagination">
      <el-pagination
        background
        layout="total,   prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        :page-size="20"
        :total="pagiData.total"
        :current-page.sync="pagiData.current"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>

import { getCateList, getSupplierList, getGoodsList } from "@/api/goodsBase";
export default {
  name: "countSales",
  props: {
    goodsId: ''
  },
  mounted() { },
  data() {
    return {
      searchform: { date: '' },
      requestSearch: {},
      supplierList: [],
      categoryList: [],
      productList: [
        {
          supplierName: "test",
          sales: 128,
          coverUrl: ""
        }
      ],
      pagiData: {
        total: 100,
        current: 0
      },
      tablePagi: {
        pageNum: 1,
        pageSize: 20
      },
      countSalesVisible: false,
      editVisible: false
    };
  },
  created() {
    //  this._refresh();
  },
  methods: {
    _refresh() {
      this.getTableList(this.tablePagi);
      getCateList().then(res => {
        if (res.data && res.data.success == true) {
          let list = res.data.data;
          this.categoryList = list.map(el => {
            return {
              value: el.category.id,
              label: el.category.name,
              children: el.detailList.map(el => {
                return { value: el.categoryId, label: el.name };
              })
            };
          });
          console.log(this.categoryList);
        } else {
          this.categoryList = [];
        }
      });

      getSupplierList().then(res => {
        let resdata = res.data;
        if (resdata && resdata.success) {
          this.supplierList = resdata.data;
        } else {
          this.supplierList = [];
        }
      });
    }, // 获取列表， 更新分页
    getTableList(queryData) {
      getGoodsList(queryData).then(res => {
        let resdata = res.data;
        if (resdata && resdata.success) {
          this.productList = resdata.data;
          this.pagiData.total = resdata.extra.total;
          console.log(this.pagiData);
        } else {
          this.productList = [];
        }
      });
    },
    // 重置分页
    handleSearch() {
      this.tablePagi = { ...this.tablePagi, pageNum: 1 };
      this.pagiData = { ...this.pagiData, current: 0 };
      if (this.searchform.categoryId && this.searchform.categoryId.length) {
        let len = this.searchform.categoryId.length;
        this.requestSearch = {
          ...this.searchform,
          categoryId: this.searchform.categoryId[len - 1]
        }
      }
      this.getTableList({ ...this.tablePagi, ...this.requestSearch })
    },
    // 重置页面， 包括搜索框，分页
    handleReset() {
      this.searchform = {};
      this.tablePagi = { pageSize: 20, pageNum: 1 };
      this.pagiData = { ...this.pagiData, current: 0 };
      this.requestSearch = {}
      this.getTableList(this.tablePagi);
    },
    handleEdit(index, row) {
      this.editVisible = true;
    },
    showSalesModal(index, row) {
      this.countSalesVisible = true;
    },
    handleCurrentChange(val) {
      this.tablePagi = { ...this.tablePagi, pageNum: val };
      this.getTableList({ ...this.tablePagi, ...this.requestSearch })
    },

    addProduct() {
      this.$router.push("addProduct");
    }
  }
};
</script>
<style scoped>
.search-title {
  font-size: 18px;
}
.pagination {
  margin: 15px 0;
}
.row-margin {
  margin: 15px 0;
}
</style>
